<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }

        $totaluser = loadModel('tyyh\\User')->count();
        $totalstore = loadModel('tysj\\Store')->where(['status' => loadEnum('StoreStatus')->code2key('jingying')])->count();
        $totalproduct = loadModel('tysj\\Product')->where(['status' => loadEnum('ProductStatus')->code2key('shangjia')])->count();
        $year = Date('Y');
        $totalorder = loadModel('jy\\Order')->where("YEAR(FROM_UNIXTIME(paytime)) = {$year}")->count();

        $paylist = loadModel('jy\\Order')
            ->whereTime('paytime', '-7 days')
            ->group('date')
            ->order('date ASC')
            ->column('FROM_UNIXTIME(paytime, "%Y-%m-%d") as date, sum(total_fee) as fee');

        $paynumlist = [];

        for ($i = 6; $i >= 0; $i--) {
            $date = date('Y-m-d', strtotime("-$i days"));
            $paynumlist[$date] = isset($paylist[$date]) ? $paylist[$date] : 0;
        }

        $todayordernum = loadModel('jy\\Order')->whereTime('paytime', 'today')->count();
        $todayorderfee = loadModel('jy\\Order')->whereTime('paytime', 'today')->sum('total_fee');
        $threedayordernum = loadModel('jy\\Order')->whereTime('paytime', '-3 days')->count();
        $threedayorderfee = loadModel('jy\\Order')->whereTime('paytime', '-3 days')->sum('total_fee');
        $sevendayordernum = loadModel('jy\\Order')->whereTime('paytime', '-7 days')->count();
        $sevendayorderfee = loadModel('jy\\Order')->whereTime('paytime', '-7 days')->sum('total_fee');
        
        $this->view->assign([
            'totaluser'         => $totaluser,
            'totalstore'        => $totalstore,
            'totalproduct'      => $totalproduct,
            'totalorder'        => $totalorder,

            'todayordernum'     => $todayordernum,
            'todayorderfee'     => $todayorderfee,
            'threedayordernum'  => $threedayordernum,
            'threedayorderfee'  => $threedayorderfee,
            'sevendayordernum'  => $sevendayordernum,
            'sevendayorderfee'  => $sevendayorderfee

        ]);

        $this->assignconfig('column', array_keys($paynumlist));
        $this->assignconfig('paynum', array_values($paynumlist));

        return $this->view->fetch();
    }

}
